<?php
namespace app\admin\logic;
use app\admin\validate\Modular as ModularValidate;

class ModularLogic extends BaseLogic {

    /**
     * 获取模块查询条件
     * @param $branchid
     * @param $floorid
     * @param $regionid
     * @return array
     */
    public function get_search_modular_maps($branchid,$floorid,$regionid){
        $maps = [];
        if($branchid != -1){
            $f_maps = [];
            $f_maps[] = ['branchid','=',$branchid];
            $floorids = model('floor')->get_column($f_maps,'id');

            $m_maps = [];
            $m_maps[] = ['floorid','in',$floorids];
            $regionids = model('region')->get_column($m_maps,'id');
            $maps[] = ['regionid','in',$regionids];
        }

        if($floorid != -1){
            $m_maps = [];
            $m_maps[] = ['floorid','=',$floorid];
            $regionids = model('region')->get_column($m_maps,'id');
            $maps[] = ['regionid','in',$regionids];
        }

        if($regionid != -1){
            $maps[] = ['regionid','=',$regionid];
        }
        return $maps;
    }

    /**
     * 检查新增模块规则
     * @param $params
     * @return mixed
     */
    public function check_add_modular_rule($params){
        $validate = new ModularValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        //$maps = [];
        //$maps[] = ['regionid','=',$params['regionid']];
        //$maps[] = ['name','=',trim($params['name'])];
        //$m_detail = model('modular')->get_detail($maps,'id');
        //if(!empty($m_detail)){
            //return $this->get_response(0,'该模块已存在');
        //}
        return $this->get_response(1,'OK');
    }

    /**
     * 检查修改模块规则
     * @param $params
     * @return mixed
     */
    public function check_save_modular_rule($params){
        $validate = new ModularValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['modularid']];
        $m_detail = model('modular')->get_detail($maps,'id');
        if(empty($m_detail)){
            return $this->get_response(0,'该模块不存在');
        }

        $maps = [];
        $maps[] = ['id','<>',$params['modularid']];
        $maps[] = ['regionid','=',$params['regionid']];
        $maps[] = ['name','=',trim($params['name'])];
        $detail = model('modular')->get_detail($maps,'id');
        if(!empty($detail)){
            return $this->get_response(0,'该模块已存在');
        }

        $data['m_detail'] = $m_detail;
        return $this->get_response(1,'OK',$data);
    }
}